import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
import { AgentService } from './agent.service';
import { AgentLoginReqDto, UpdateClerkProfileDto } from './agent.dto';
import { AuthService } from 'src/auth/auth.service';
import { AuthResDto } from 'src/auth/auth.dto';
import { ClerkId } from './agent.decorator';
import { ClerkStatus } from 'src/core/entities/clerk.entity';
import { ApiOperation } from '@nestjs/swagger';

@Controller('agent')
export class AgentController {
  constructor(
    private readonly authService: AuthService,
    private readonly agentService: AgentService,
  ) {}

  @Post('/login')
  @ApiOperation({
    summary: '客服登录',
    description: '使用`手机号码`+`密码`登录',
  })
  async login(@Body() data: AgentLoginReqDto): Promise<AuthResDto> {
    const { phone, password } = data;
    return await this.authService.validateClerk(phone, password);
  }

  @Get('/profile/info')
  @ApiOperation({
    summary: '获取客服资料',
    description: '需携带访问凭证',
    tags: ['profile'],
  })
  async getProfileInfo(@ClerkId() clerkId: number) {
    return await this.agentService.getClerkProfile(clerkId);
  }

  @Post('/profile/status')
  @ApiOperation({
    summary: '修改客服状态',
    description: '`status`: [`online`,`hidden`,`offline`]',
    tags: ['profile'],
  })
  async changClerkStatus(@ClerkId() clerkId: number, status: ClerkStatus) {
    return await this.agentService.switchClerkStatus(clerkId, status);
  }

  @Post('/profile/info')
  @ApiOperation({
    summary: '修改客服资料',
    description: '修改客服`头像`,`昵称`等',
    tags: ['profile'],
  })
  async setProfileInfo(
    @ClerkId() clerkId: number,
    data: UpdateClerkProfileDto,
  ) {
    return await this.agentService.setClerkProfile(clerkId, data);
  }

  @Get('/orders')
  @ApiOperation({ summary: '获取客户订单记录', tags: ['order'] })
  async getUserOrderList(@Query() userId: number) {
    return await this.agentService.findUserOrderList(userId);
  }

  @Get('/orders/:id')
  @ApiOperation({ summary: '获取订单详情', tags: ['order'] })
  async getUserOrderDetails(@Param('id') id: number) {
    return await this.agentService.findUserOrderDetail(id);
  }
}
